//
//  BMSAntiAddictionManager.m
//  WDBMS
//
//  Created by YeJary on 2025/5/13.
//

#import "BMSAntiAddictionManager.h"
#import "BMSInitializer.h"
@interface BMSAntiAddictionManager()


@end

@implementation BMSAntiAddictionManager

+ (instancetype)shared {
    static BMSAntiAddictionManager *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (BOOL)isTodayHoliday {
    NSString *today = [self currentDateString];
    return [self.holidaySet containsObject:today];
}

- (BOOL)isCurrentTimeInPlayWindow {
    NSDate *now = [NSDate date];
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday | NSCalendarUnitHour fromDate:now];

    BOOL isWeekend = (comps.weekday == 1 || comps.weekday == 7); // 1=Sunday, 7=Saturday
    BOOL isHoliday = [self isTodayHoliday];
    BOOL isAllowedHour = (comps.hour == 20);

    return (isHoliday || isWeekend) && isAllowedHour;
}

- (BOOL)shouldForceLogoutForTeenager {
    BOOL isTeenager = [[NSUserDefaults standardUserDefaults] boolForKey:@"bms.isTeenager"];
    if (!isTeenager) return NO;

    if (![self isCurrentTimeInPlayWindow]) {
        return YES;
    }
    return NO;
}

- (NSString *)currentDateString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMdd";
    return [formatter stringFromDate:[NSDate date]];
}

- (BOOL)isAllowedToPlay {
    if (![BMSInitializer shared].isTeenager) {
        return YES; // 成年人无限制
    }

    if (![self isTodayHoliday]) {
        return NO; // 非节假日，未成年不可登录
    }

    if (![self isCurrentTimeInPlayWindow]) {
        return NO; // 非允许时间段
    }

    return YES; // 满足防沉迷要求
}

@end
